#include "triangle.h"

#include <cstring>
#include <iostream>

Triangle::Triangle(std::vector<double> &_data)
    : Figure::Figure(_data)
{

}

void Triangle::setData(std::vector<double> &_data)
{
    Figure::setData(_data);
}

std::vector<double> Triangle::getData()
{
    return Figure::getData();
}

std::pair<double, double> Triangle::getCentrePoint()
{
    // coordinate of side's center
    double data6 = (data[0] + data[2]) / 2;
    double data7 = (data[1] + data[3]) / 2;

    // coordinate of triangle's center of mass
    if (data[4] < data6)
        data6 = data[4] + 2 * (data6 - data[4]) / 3;
    else
        data6 = data6 + (data[4] - data6) / 3;

    if (data[5] < data7)
        data7 = data[5] + 2 * (data7 - data[5]) / 3;
    else
        data7 = data7 + (data[5] - data7) / 3;

    return std::pair<double, double>(data6, data7);
}

void Triangle::extension(double coefficient)
{
    // new coordinates of vertex
    std::pair<double, double> center = getCentrePoint();
    for (size_t i = 0; i < data.size() - 1; ++i) {
        if (i % 2 == 0) {
            (data[i] < center.first) ? (data[i] = center.first - coefficient * (center.first - data[i])) :
                                       (data[i] = center.first + coefficient * (data[i] - center.first));
        } else {
            (data[i] < center.second) ? (data[i] = center.second - coefficient * (center.second - data[i])) :
                                       (data[i] = center.second + coefficient * (data[i] - center.second));
        }
    }
}

void Triangle::shift(double coefficient1, double coefficient2)
{
    data[0] += coefficient1;
    data[1] += coefficient2;

    data[2] += coefficient1;
    data[3] += coefficient2;

    data[4] += coefficient1;
    data[5] += coefficient2;
}

void Triangle::turn(double angle)
{
    Figure::turn(angle);
}

FigureType Triangle::type()
{
    return tri;
}

Triangle::~Triangle()
{

}
